package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> itemQueryPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        Page<Item> itemPage = itemService.page(new Page<>(page, size));
        return new PageDTO<>(itemPage.getTotal(), itemPage.getRecords());
    }

    /**
     * 新增
     *
     * @param item
     */
    @PostMapping
    public void itSave(@RequestBody Item item) {
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(2);
        itemService.save(item);
    }

    /**
     * 修改商品
     * @param item
     */
    @PutMapping
    public void updateItem(@RequestBody Item item) {
        item.setUpdateTime(new Date());
        item.setStatus(null);
        itemService.updateById(item);
    }

    /**
     * 上下架商品
     *
     * @param id
     * @param status
     */
    @PutMapping("status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        itemService.itUpdate(id, status);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable("id") Long id) {
        Item byId = itemService.getById(id);
        return byId;
    }

    /**
     * 根据id删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public  void deleteItem(@PathVariable("id")Long id){
        itemService.removeById(id);
    }
}
